package com.tianqu.tms.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dao.CommercialVehicleDao;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dto.CommercialVehicleDto;

public class CommercialVehicle extends TmsEntity{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2283632131553669706L;

	@Override
	public void save() throws TmsDaoException, TmsException {
		// TODO Auto-generated method stub
		
	}
	
	private String id;//款式id
	private String vendorName;//品牌名称
	private String brandName;//车系名称
	private String modelName;//款式名称
	private String vendorId;//品牌id
	private String brandId;//车系id
	
	/**
	 * 实例化codes表
	 * 
	 * @return
	 */
	private static CommercialVehicle instance;

	List<CommercialVehicle> codes = null;
	List<CommercialVehicle> allcodes = new ArrayList<CommercialVehicle>();
	
	/**
	 * 加载Codes
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public static CommercialVehicle getInstance() throws TmsDaoException {
		if (null == instance) {
			instance = new CommercialVehicle();
			instance.init();
		}
		return instance;
	}

	/**
	 * 实例化Code
	 * 
	 * @throws TmsDaoException
	 */
	public void init() throws TmsDaoException {
		codes = new ArrayList<CommercialVehicle>();
		setCodes();
	}
	
	/**
	 * 获取Codes信息存在静态codes中
	 * @throws TmsDaoException
	 */
	public void setCodes() throws TmsDaoException {
		CommercialVehicleDao dao = (CommercialVehicleDao) TmsDao.getInstance(TmsDao.TMS_COMMERCIAL_VEHICLE);
		List<CommercialVehicleDto> list = dao.loadCommercialVendor();
		for (int i = 0; i < list.size(); i++) {
			CommercialVehicleDto dto = (CommercialVehicleDto) list.get(i);
			CommercialVehicle entity = new CommercialVehicle();
			entity.setBrandId(dto.getBrandId());
			entity.setBrandName(dto.getBrandName());
			entity.setVendorId(dto.getVendorId());
			entity.setId(dto.getId());
			entity.setModelName(dto.getModelName());
			entity.setVendorName(dto.getVendorName());
			codes.add(entity);
		}
		
		//
		List<CommercialVehicleDto> all = dao.loadAllCommercialVendor();
		for (int i = 0; i < all.size(); i++) {
			CommercialVehicleDto dto = (CommercialVehicleDto) all.get(i);
			CommercialVehicle entity = new CommercialVehicle();
			entity.setBrandId(dto.getBrandId());
			entity.setBrandName(dto.getBrandName());
			entity.setVendorId(dto.getVendorId());
			entity.setId(dto.getId());
			entity.setModelName(dto.getModelName());
			entity.setVendorName(dto.getVendorName());
			allcodes.add(entity);
		}
	}

	/**
	 * 根据款式ID获取车辆名称
	 * @return
	 */
	
	public String getCarStyleRNameById(String carStyleId){
		
		for(int i=0;i<allcodes.size();i++)
		{
			CommercialVehicle entity = (CommercialVehicle)allcodes.get(i);
			if(entity.getId().equals(carStyleId))
			{
				return entity.getModelName();
			}
		}
		return "";
	}
	/**
	 * 获取下拉列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List findAllVendorInfo(){
		List list = new ArrayList();
		Map map = null;
		for(int i=0;i<codes.size();i++){
			CommercialVehicle commercialVehicle = codes.get(i);
			map = new HashMap();
			map.put("vendorId", commercialVehicle.getVendorId());
			map.put("vendorName", commercialVehicle.getVendorName());
			list.add(map);
		}
		return list;
	}
	
	/**
	 * 根据品牌ID获取车系下拉列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List getAllVehicleBrandInfo(String vendorId)
	{
		List list = new ArrayList();
		Map map = null;
		for(int i=0;i<codes.size();i++)
		{
			CommercialVehicle entity = codes.get(i);
			if(entity.getVendorId().equals(vendorId))
			{
				map = new HashMap();
				map.put("brandId", entity.getBrandId());
				map.put("brandName", entity.getBrandName());
				list.add(map);
			}
		}
		return list;
	}
	
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getVendorName() {
		return vendorName;
	}

	public void setVendorName(String vendorName) {
		this.vendorName = vendorName;
	}

	public String getBrandName() {
		return brandName;
	}

	public void setBrandName(String brandName) {
		this.brandName = brandName;
	}

	public String getModelName() {
		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

	public String getVendorId() {
		return vendorId;
	}

	public void setVendorId(String vendorId) {
		this.vendorId = vendorId;
	}

	public String getBrandId() {
		return brandId;
	}

	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	
	/**
	 * 
	 */
	public List getVehicleBrandInfo(String vendorId)
	{
		List list = new ArrayList();
		Map map = new HashMap();
		for(int i=0;i<allcodes.size();i++)
		{
			CommercialVehicle entity = allcodes.get(i);
			if(entity.getVendorId().equals(vendorId))
			{
				if(map.containsKey(entity.getBrandId())) continue;
				map.put(entity.getBrandId(), entity.getBrandName());
				
				Map m = new HashMap();
				m.put("brandId", entity.getBrandId());
				m.put("brandName", entity.getBrandName());
				list.add(m);
			}
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List findVendorInfo(){
		List list = new ArrayList();
		Map map = new HashMap();
		for(int i=0;i<allcodes.size();i++){
			CommercialVehicle commercialVehicle = allcodes.get(i);
			if(map.containsKey(commercialVehicle.getVendorId())) continue;
			map.put(commercialVehicle.getVendorId(), commercialVehicle.getVendorName());
			
			Map m = new HashMap();
			m.put("vendorId", commercialVehicle.getVendorId());
			m.put("vendorName", commercialVehicle.getVendorName());
			list.add(m);
		}
		
		return list;
	}
	
	/**
	 * 根据车系ID获取款式下拉列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List getAllStyleInfo(String brandId)
	{
		List list = new ArrayList();
		Map map = null;
		for(int i=0;i<allcodes.size();i++)
		{
			CommercialVehicle entity = allcodes.get(i);
			if(entity.getBrandId().equals(brandId))
			{
				map = new HashMap();
				map.put("carStyleId", entity.getId());
				map.put("carStyleName", entity.getModelName());
				list.add(map);
			}
		}
		return list;
	}
	
}


